#!/usr/bin/python
# -*- coding: utf-8 -*-
# @name             : 发送邮件
# @author           : OneYoung
# @create           : 2024/02/04
# @version          : v1.0
# @desc             : 发送邮件封装类

import yagmail
from typing import List
from application.settings import EMAIL_CONFIG


class Email:
    @staticmethod
    def send(subject: str, body: any, attachments: List[str] = None):
        """
        发送邮件
        :param subject: 邮件主题
        :param body: 邮件内容
        :param attachments: 附件列表
        """
        # 连接邮箱服务
        server = yagmail.SMTP(
            user=EMAIL_CONFIG['user'],
            password=EMAIL_CONFIG['password'],
            host=EMAIL_CONFIG['host'],
            port=EMAIL_CONFIG['port'],
        )
        try:
            print("开始发送邮件...")
            server.send(
                to=EMAIL_CONFIG['to_addrs'],
                subject=subject,
                contents=body,
                attachments=attachments
            )
            print("邮件发送成功!")
            return True
        except Exception as e:
            print(f"邮件发送失败！错误信息：{e}")
            return False
